package com.nahan.secondapplication.LottieAnimation;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.nahan.secondapplication.R;

/**
 * 里面需要的json动画  需要Ae软件导出
 * @CreateDate: 2020-8-20 16:38
 */
public class LottieActivity extends AppCompatActivity implements View.OnClickListener {

    private LottieTabView mLottieMainTab,mLottieMsgTab,mLottieDealTab,mLottieMineTab,tab_view_min1;
    private FrameLayout homeFrame;
    private CustomFragment customFragment;
    private CustomFragment1 customFragment1;
    private CustomFragment2 customFragment2;
    private CustomFragment3 customFragment3;
    private CustomFragment4 customFragment4;

    /**
     * @Fields mIsBack : 双击退出程序参数
     */
    private boolean mIsBack = false;
    private long mBackTime = 0;
    private int mDeltaTime = 1000;
    private Handler handlerback=new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);

        mLottieMainTab = findViewById(R.id.tab_view_main);
        mLottieMsgTab = findViewById(R.id.tab_view_msg);
        mLottieDealTab = findViewById(R.id.tab_view_deal);
        mLottieMineTab = findViewById(R.id.tab_view_mine);
        tab_view_min1 = findViewById(R.id.tab_view_min1);
        homeFrame = findViewById(R.id.homeFrame);


        mLottieMainTab.setOnClickListener(this);
        mLottieMsgTab.setOnClickListener(this);
        mLottieDealTab.setOnClickListener(this);
        mLottieMineTab.setOnClickListener(this);
        tab_view_min1.setOnClickListener(this);


        /**
         * 默认展示页面
         */
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        customFragment =new CustomFragment();
        transaction.add(R.id.homeFrame, customFragment);
        rlSetSelect(true, false, false, false, false);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void onClick(View view) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (view.getId()) {
            case R.id.tab_view_main:
                rlSetSelect(true, false, false, false, false);
                if (null == customFragment) {
                    customFragment =new CustomFragment();
                    transaction.add(R.id.homeFrame, customFragment);
                } else {
                    transaction.show(customFragment);
                }
                break;
            case R.id.tab_view_msg:

                rlSetSelect(false, true, false, false, false);
                if (null == customFragment1) {
                    customFragment1 =new CustomFragment1();
                    transaction.add(R.id.homeFrame, customFragment1);
                } else {
                    transaction.show(customFragment1);
                }
                break;
            case R.id.tab_view_deal:
                rlSetSelect(false, false, true, false, false);
                if (null == customFragment2) {
                    customFragment2 = new CustomFragment2();
                    transaction.add(R.id.homeFrame, customFragment2);
                } else {
                    transaction.show(customFragment2);
                }
                break;
            case R.id.tab_view_mine:
                rlSetSelect(false, false, false, true, false);
                if (null == customFragment3) {
                    customFragment3 =new CustomFragment3();
                    transaction.add(R.id.homeFrame, customFragment3);
                } else {
                    transaction.show(customFragment3);
                }
                break;
            case R.id.tab_view_min1:
                rlSetSelect(false, false, false, false, true);
                if (null == customFragment4) {
                    customFragment4 =new CustomFragment4();
                    transaction.add(R.id.homeFrame, customFragment4);
                } else {
                    transaction.show(customFragment4);
                }
                break;
                default:
                    break;
        }
        transaction.commit();
    }


    /**
     * 选中状态
     */
    public void rlSetSelect(boolean select1, boolean select2, boolean select3, boolean select4, boolean select5) {
        mLottieMainTab.selected(select1);
        mLottieMsgTab.selected(select2);
        mLottieDealTab.selected(select3);
        mLottieMineTab.selected(select4);
        tab_view_min1.selected(select5);
    }

    /**
     * 隐藏fragment
     *
     * @param transaction
     */
    public void hideFragment(FragmentTransaction transaction) {

        if (null != customFragment) {
            transaction.hide(customFragment);
        }

        if (null != customFragment1) {
            transaction.hide(customFragment1);
        }

        if (null != customFragment2) {
            transaction.hide(customFragment2);
        }

        if (null != customFragment3) {
            transaction.hide(customFragment3);
        }
        if (null != customFragment4) {
            transaction.hide(customFragment4);
        }
    }

    /**
     * @Title: onKeyDown
     * @Description: 双击后退健缩小应用
     */
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            if (mIsBack == false) {
//                mBackTime = System.currentTimeMillis();
//                mIsBack = true;
//                Toast.makeText(getApplicationContext(),"再按一次缩小应用",Toast.LENGTH_SHORT).show();
//                handlerback.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        mIsBack = false;
//                    }
//
//                }, 2000);
//            } else if (mBackTime + mDeltaTime > System.currentTimeMillis()) {
//                Intent intent = new Intent(Intent.ACTION_MAIN);
//                intent.addCategory(Intent.CATEGORY_HOME);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent);
//                // System.exit(0);
//            }
//            return false;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

}
